package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.AdminUserFlowing;
import com.ruoyi.system.domain.AdminWeixinUser;
import com.ruoyi.system.mapper.AdminUserFlowingMapper;
import com.ruoyi.system.mapper.AdminWeixinUserMapper;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AdminUserRechargeRecordMapper;
import com.ruoyi.system.domain.AdminUserRechargeRecord;
import com.ruoyi.system.service.IAdminUserRechargeRecordService;

/**
 * 用户充值记录Service业务层处理
 *
 * @author ruoyi
 * @date 2020-11-06
 */
@Service
public class AdminUserRechargeRecordServiceImpl implements IAdminUserRechargeRecordService
{
    @Autowired
    private AdminUserRechargeRecordMapper adminUserRechargeRecordMapper;

    @Autowired
    private AdminWeixinUserMapper adminWeixinUserMapper;

    @Autowired
    private AdminUserFlowingMapper adminUserFlowingMapper;

    /**
     * 查询用户充值记录
     *
     * @param id 用户充值记录ID
     * @return 用户充值记录
     */
    @Override
    public AdminUserRechargeRecord selectAdminUserRechargeRecordById(Long id)
    {
        return adminUserRechargeRecordMapper.selectAdminUserRechargeRecordById(id);
    }

    /**
     * 查询用户充值记录列表
     *
     * @param adminUserRechargeRecord 用户充值记录
     * @return 用户充值记录
     */
    @Override
    public List<AdminUserRechargeRecord> selectAdminUserRechargeRecordList(AdminUserRechargeRecord adminUserRechargeRecord)
    {
        return adminUserRechargeRecordMapper.selectAdminUserRechargeRecordList(adminUserRechargeRecord);
    }

    /**
     * 新增用户充值记录
     *
     * @param adminUserRechargeRecord 用户充值记录
     * @return 结果
     */
    @Override
    public int insertAdminUserRechargeRecord(AdminUserRechargeRecord adminUserRechargeRecord)
    {
        adminUserRechargeRecord.setCreateTime(DateUtils.getNowDate());
        //记录用户流水
        AdminUserFlowing userFlowing = new AdminUserFlowing();
        userFlowing.setCreateTime(DateUtils.getNowDate());
        userFlowing.setUserId(adminUserRechargeRecord.getUserId());
        userFlowing.setWexinId(adminUserRechargeRecord.getWexinId());
        userFlowing.setWexinName(adminUserRechargeRecord.getWexinName());
        userFlowing.setRoom(adminUserRechargeRecord.getRoom());
//        userFlowing.setPeriod(prizeRecordOne.getPeriod());
        userFlowing.setPayType(2);
        userFlowing.setIsFlowing(0);
        AdminWeixinUser user = new AdminWeixinUser();
        user.setRoom(adminUserRechargeRecord.getRoom());
        user.setWexinId(adminUserRechargeRecord.getWexinId());
        AdminWeixinUser userInfo = adminWeixinUserMapper.selectAdminWeixinUserByWeChatIdAndRoom(user);
        userFlowing.setStartPrice(userInfo.getPrice());
        userFlowing.setPayPrice(adminUserRechargeRecord.getRechargePrice());
        userFlowing.setEndPrice(userInfo.getPrice().add(adminUserRechargeRecord.getRechargePrice()));
        userFlowing.setRemark("管理员充值");
        adminUserFlowingMapper.insertAdminUserFlowing(userFlowing);
        return adminUserRechargeRecordMapper.insertAdminUserRechargeRecord(adminUserRechargeRecord);
    }

    /**
     * 修改用户充值记录
     *
     * @param adminUserRechargeRecord 用户充值记录
     * @return 结果
     */
    @Override
    public int updateAdminUserRechargeRecord(AdminUserRechargeRecord adminUserRechargeRecord)
    {
        adminUserRechargeRecord.setUpdateTime(DateUtils.getNowDate());
        return adminUserRechargeRecordMapper.updateAdminUserRechargeRecord(adminUserRechargeRecord);
    }

    /**
     * 批量删除用户充值记录
     *
     * @param ids 需要删除的用户充值记录ID
     * @return 结果
     */
    @Override
    public int deleteAdminUserRechargeRecordByIds(Long[] ids)
    {
        return adminUserRechargeRecordMapper.deleteAdminUserRechargeRecordByIds(ids);
    }

    /**
     * 删除用户充值记录信息
     *
     * @param id 用户充值记录ID
     * @return 结果
     */
    @Override
    public int deleteAdminUserRechargeRecordById(Long id)
    {
        return adminUserRechargeRecordMapper.deleteAdminUserRechargeRecordById(id);
    }
}
